#include <stdio.h>

#include <pthread.h>

#include "bigiot.h"

#if 0
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
#else
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif

void *pthread_cmd(void *);
void *pthread_beat(void *);
void *pthread_update(void *);

struct task {
	pthread_t tidp;
	int micro_seconds;
	void * (*start_rtn)(void *);
};

struct task task_tbl[] = {
	{ 0,   100000, pthread_cmd },
    { 0, 30000000, pthread_beat },
	{ 0, 60000000, pthread_update },
};

int main(int argc, char *argv[])
{
    int i, ret;

    ret = init_bigiot(NULL);
    if (0 == ret)
    {
        printf("init_bigiot\n");
    }

	for (i = 0; i < ARRAY_SIZE(task_tbl); i ++)
    {
		ret = pthread_create(&task_tbl[i].tidp,
							NULL,
							task_tbl[i].start_rtn,
							&task_tbl[i].micro_seconds);

		if (ret)
        {
			printf("Create pthread error:%d\n", i);
		}
	}

	for (i = 0; i < sizeof(task_tbl) / sizeof(task_tbl[0]); i ++)
    {
		pthread_join(task_tbl[i].tidp, NULL);
	}

	return 0;
}

void *pthread_cmd(void *arg)
{
    int ret;

    while (1)
    {
        ret = period_handler_cmd(NULL);

		usleep(*(int *)arg);
	}

	return NULL;
}

void *pthread_beat(void *arg)
{
    int ret;

    while (1)
    {
        ret = period_handler_beat(NULL);

		usleep(*(int *)arg);
	}

	return NULL;
}

void *pthread_update(void *arg)
{
    int ret;

    while (1)
    {
        ret = period_handler_update(NULL);

		usleep(*(int *)arg);
	}

	return NULL;
}
